
//
//  WSForumDetailsViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/19.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSForumDetailsViewController.h"
#import "WSForumDetailsCell.h"
#import "WSForumModel.h"
#import "WSAFNetWorkingTool.h"
#import "WSMyTool.h"
@interface WSForumDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *arrComments;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrUser;

@end

@implementation WSForumDetailsViewController
- (NSMutableArray *)arrComments {
    if (!_arrComments) {
        self.arrComments = [NSMutableArray array];
    }
    return _arrComments;
}
- (NSMutableArray *)arrUser {
    if (!_arrUser) {
        self.arrUser  = [NSMutableArray array];
    }
    return _arrUser;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self getData];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:0];
    [self.tableView registerClass:[WSForumDetailsCell class] forCellReuseIdentifier:NSStringFromClass([WSForumDetailsCell class])];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}


- (void)getData {
    NSString *url = [NSString stringWithFormat:@"http://api.fit-time.cn/ftsns/refreshGroupTopicComments?topic_id=%@&page_size=25", self.strID];
    [WSAFNetWorkingTool getWithURL:url parameter:nil httpHeader:nil responsType:ResponseTypeJSON progress:nil success:^(id result) {
        NSArray *arrComments = result[@"comments"];
        for (NSDictionary *dic in arrComments) {
            WSForumModel *model = [[WSForumModel alloc] initWithDic:dic];
            [self.arrComments addObject:model];
            NSString *url = [NSString stringWithFormat:@"http://api.fit-time.cn/ftuser/loadUserProfile?user_id=%@", model.userId];
            [WSAFNetWorkingTool getWithURL:url parameter:nil httpHeader:nil responsType:ResponseTypeJSON progress:nil success:^(id result) {
                NSDictionary *dicUser = result[@"user"];
                WSForumModel *model = [[WSForumModel alloc] initWithDic:dicUser];
                [self.arrUser addObject:model];
                [self.tableView reloadData];
            } failure:^(NSError *error) {
                
            }];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrUser.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WSForumDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSForumDetailsCell class])];
    if (0 != self.arrUser.count ) {
        WSForumModel *modelUse = [self.arrUser objectAtIndex:indexPath.row];
        cell.modelUse = modelUse;
    }
    WSForumModel *modelHot = self.arrComments[indexPath.row];
    
    cell.modelHot = modelHot;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WSForumModel *model = self.arrComments[indexPath.row];
    CGFloat heightRow = [WSAFNetWorkingTool getSuitableHeightOfLableContentString:model.comment OringeWidth:394 Font:[UIFont systemFontOfSize:17]];
    return heightRow + 90;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
